package drawables;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.ArrayList;


public class EffectUtils {
	public static float[] screenPixels(int ... pixels){
		int r = 0, g = 0, b = 0;
		for(int pixel : pixels){
			Color c = new Color(pixel);
			r = Math.max(r, c.getRed());
			g = Math.max(g, c.getGreen());
			b = Math.max(b, c.getBlue());
		}
		return Color.RGBtoHSB(Math.min(r, 255), Math.min(g, 255), Math.min(b, 255), null);
	}
	
	public static int screenPixelsRGB(int ... pixels){
		int r = 0, g = 0, b = 0;
		for(int pixel : pixels){
			Color c = new Color(pixel);
			r = Math.max(r, c.getRed());
			g = Math.max(g, c.getGreen());
			b = Math.max(b, c.getBlue());
		}
		return new Color(Math.min(r, 255), Math.min(g, 255), Math.min(b, 255)).getRGB();
	}
	
	public static BufferedImage maskImage(BufferedImage base, BufferedImage overlay, float[] alpha){
		int[] basePixels = getPixels(base);
		int[] overPixels = getPixels(overlay);
		for(int i=0; i<basePixels.length; i++){
			overPixels[i] = fadePixel(basePixels[i], overPixels[i], alpha[i]);
		}
		setPixels(overPixels, overlay);
		return overlay;
	}
	
	public static int fadePixel(int base, int overlay, float alpha){
		Color bCol = new Color(base);
		Color oCol = new Color(overlay);
		float iAlpha = 1 - alpha;
		int r = (int)(bCol.getRed()*iAlpha + oCol.getRed()*alpha);
		int g = (int)(bCol.getGreen()*iAlpha + oCol.getGreen()*alpha);
		int b = (int)(bCol.getBlue()*iAlpha + oCol.getBlue()*alpha);
		return new Color(r, g, b).getRGB();
	}
	
	public static BufferedImage screenImages(BufferedImage ... images){
		ArrayList<int[]> allPixels = new ArrayList<int[]>(images.length);
		for(BufferedImage image : images)
			allPixels.add(getPixels(image));
		BufferedImage out = images[images.length-1];
		int[] outPixels = getPixels(out);
		for(int i=0; i<outPixels.length; i++){
			int[] pixs = new int[images.length];
			for(int j=0; j<images.length; j++){
				pixs[j] = allPixels.get(j)[i];
			}
			int combined = screenPixelsRGB(pixs);
			outPixels[i] = combined;
		}
		setPixels(outPixels, out);
		return out;
	}
	
	public static BufferedImage addImages(BufferedImage ... images){
		ArrayList<int[]> allPixels = new ArrayList<int[]>(images.length);
		for(BufferedImage image : images)
			allPixels.add(getPixels(image));
		BufferedImage out = images[images.length-1];
		int[] outPixels = getPixels(out);
		for(int i=0; i<outPixels.length; i++){
			int[] pixs = new int[images.length];
			for(int j=0; j<images.length; j++){
				pixs[j] = allPixels.get(j)[i];
			}
			int combined = addPixels(pixs);
			outPixels[i] = combined;
		}
		setPixels(outPixels, out);
		return out;
	}
	
	public static int addPixels(int ... pixels){
		int r = 0, g = 0, b = 0;
		for(int pixel : pixels){
			Color c = new Color(pixel);
			r += c.getRed();
			g += c.getGreen();
			b += c.getBlue();
		}
		return new Color(Math.min(r, 255), Math.min(g, 255), Math.min(b, 255)).getRGB();
	}
	
	public static int[] getPixels(BufferedImage image){
		return image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
	}
	
	public static void setPixels(int[] pixels, BufferedImage image){
		image.setRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
	}
	
	public static void enableAntialias(Graphics2D g2d){
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
	}
}
